package edu.jvn.ict.locationsharing;

import java.net.SocketException;
import java.util.ArrayList;

import android.content.Context;
import android.widget.Toast;
import edu.jvn.ict.locationsharing.informationexchange.JoiningGroupMessage;

public class Utility {

	public static final String APP_START_MESSAGE = "*#ls#*";
	public static final String JOINING_GROUP_REQUEST = "hello";

	public static String generateSendingMessage(Object message) {
		return APP_START_MESSAGE + message.toString();
	}

	public static boolean validateReceiveMessage(String message) {
		if (message.substring(0, 6).equals(APP_START_MESSAGE)) {
			return true;
		}
		return false;
	}

	public static int getMessageType(String message) {
		String substring = message.substring(6, message.indexOf(","));
		return Integer.parseInt(substring);
	}

	public static String getMessageContent(String message) {
		return message.substring(message.indexOf(",") + 1);
	}

	public static String ipToString(int ip) {
		String ipString = "";
		int temp;

		for (int i = 3; i >= 0; --i) {
			temp = ip >> i * 8;
			ipString = ipString + temp;

			if (i != 0) {
				ipString += ".";
			}

			ip = ip - (temp << i * 8);
		}

		return ipString;
	}

	public static int ipToInt(String ipString) {
		String[] splits = ipString.split("\\.");

		int ipInt = 0;

		for (int i = 0; i < splits.length; ++i) {
			ipInt = (ipInt << 8) + Integer.valueOf(splits[i]);
		}

		return ipInt;
	}

	public static int getCID(int row, int col) {
		int cid = (row + 1) * (row + 1) - Math.min(row, col) - 1;
		if (col > row)
			cid += col * col - row * row;
		return cid;
	}

	public static int[] getRowCol(int cid) {
		int[] rowCol = new int[2];

		int col = (int) Math.floor(Math.sqrt(cid));
		int row = 0;
		int i = col * col;
		if ((cid - i) < col) {
			row += cid - i;
		} else {
			row += col;
			col -= cid - i - col;
		}

		rowCol[0] = row;
		rowCol[1] = col;

		return rowCol;
	}

	public static String getLocalIpAddress() throws SocketException {
//		for (Enumeration<NetworkInterface> en = NetworkInterface
//				.getNetworkInterfaces(); en.hasMoreElements();) {
//			NetworkInterface intf = en.nextElement();
//			for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
//					.hasMoreElements();) {
//				InetAddress inetAddress = enumIpAddr.nextElement();
//				if (!inetAddress.isLoopbackAddress()) {
//					return inetAddress.getHostAddress().toString();
//				}
//			}
//		}
//		return null;
		
		return "10.0.2.2";
	}

	public static void sendSMS(String phoneNumber, String message) {
		new Thread(new SendSMSRunnable(phoneNumber, message)).start();
	}

	// public static long getMyPhoneNumber() {
	// TelephonyManager mTelephonyMgr;
	// mTelephonyMgr = (TelephonyManager)
	// MemberServer.activity.getSystemService(Context.TELEPHONY_SERVICE);
	//
	// return Long.valueOf(mTelephonyMgr.getLine1Number());
	// return 986611241L;
	// }

	public static void createSampleSMS(Context context) {
		ArrayList<JoiningGroupMessage> joiningGroupMessagesList = new ArrayList<JoiningGroupMessage>();
		
		JoiningGroupMessage joiningGroupReceiveMessage = new JoiningGroupMessage(
				Utility.getMessageContent("*#ls#*4,-86283242,12324,6999"));
		joiningGroupReceiveMessage.setPhoneNumber(986611241);
		if (joiningGroupReceiveMessage != null) {
			joiningGroupMessagesList.add(joiningGroupReceiveMessage);
			MemberServer.exchangeInformation(joiningGroupReceiveMessage, 1,
					true);
		}

		// update members dictionary
		for (JoiningGroupMessage joiningGroupMessage : joiningGroupMessagesList) {
			MemberInfo memberInfo = MemberServer
					.createMemberFromJGM(joiningGroupMessage);
			MemberServer.addToDictionary(memberInfo);
		}

		// Send back the current members dictionary to the new
		// members
		for (JoiningGroupMessage joiningGroupMessage : joiningGroupMessagesList) {
			MemberServer.sendDictionary(joiningGroupMessage.getIp(), joiningGroupMessage.getPort());
		}
		Toast.makeText(context, "" + MemberServer.dictionary.size(),
				Toast.LENGTH_SHORT).show();
		// Toast.makeText(context, "Message sent.",
		// Toast.LENGTH_SHORT).show();
	}
}
